package Q108;

/**
 * 将有序数组转为二叉搜索树
 *
 * @author 23737
 * @time 2021.11.29
 */
public class Test {
    public static void main(String[] args) {

    }
}

class TreeNode {
    int val;
    TreeNode left;
    TreeNode right;

    TreeNode() {
    }

    TreeNode(int val) {
        this.val = val;
    }

    TreeNode(int val, TreeNode left, TreeNode right) {
        this.val = val;
        this.left = left;
        this.right = right;
    }
}

/**
 * 递归
 */
class Solution {
    public TreeNode sortedArrayToBST(int[] nums) {
        TreeNode treeNode = recursiveTree(nums,0,nums.length-1);
        return treeNode;
    }

    private TreeNode recursiveTree(int[] nums, int left, int right) {
        if (left > right){
            return null;
        }
        int mid = left+((right - left)>>1);
        TreeNode treeNode = new TreeNode(nums[mid]);
        treeNode.left = recursiveTree(nums,left,mid-1);
        treeNode.right = recursiveTree(nums,mid+1, right);
        return treeNode;
    }
}

